package com.bootdo.coach.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.coach.domain.TabDevice;
import com.bootdo.coach.domain.TabUser;
import com.bootdo.coach.service.TabDeviceService;
import com.bootdo.coach.service.TabUserMsgService;
import com.bootdo.coach.service.TabUserService;
import com.bootdo.coach.vo.TabDeviceVo;
import com.bootdo.coach.vo.TabUserVo;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

import io.swagger.annotations.ApiOperation;

@RequestMapping("/deviceUser/deviceUser")
@Controller
public class DeviceUserController extends BaseController{
	private String prefix = "msg/user";

	@Autowired
	private TabDeviceService deviceService;
	
	@Autowired
	private TabUserService userService;
	
	@Autowired
	private TabUserMsgService userMsgService;
	
	@RequiresPermissions("deviceUser:deviceUser:list")
	@GetMapping("list")
	String list(Model model) {
		return prefix + "/list";
	}
	
	@ApiOperation(value="获取设备用户列表", notes="")
	@ResponseBody
	@GetMapping("/dataList")
	@RequiresPermissions("deviceUser:deviceUser:list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
        Query query = new Query( params );
		List<TabUserVo> list = userService.list(query);
		int total = userService.count(query);
		PageUtils pageUtil = new PageUtils(list, total);
		return pageUtil;
	}
	
	private List<TabDeviceVo> getAllDevice(Long userId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", userId);
		List<TabDeviceVo> devices = deviceService.list(paramMap);
		if (devices == null) {
			devices = new ArrayList<TabDeviceVo>();
		}
		TabDeviceVo one = new TabDeviceVo();
		one.setId(0l);
		one.setDeviceName("请选择");
		devices.add(0, one);
		return devices;
	}

	@RequiresPermissions("deviceUser:deviceUser:addUser")
	@Log("添加设备用户")
	@GetMapping("/addUser")
	String addUser(Model model) {
		model.addAttribute("devices", getAllDevice(0l));
		return prefix + "/add";
	}

	@RequiresPermissions("deviceUser:deviceUser:updateUser")
	@Log("编辑设备用户")
	@GetMapping("/updateUser/{id}")
	String updateUser(Model model, @PathVariable("id") Long id) {
		model.addAttribute("devices", getAllDevice(id));
		TabUser user = userService.selectByPrimaryKey(id);
		model.addAttribute("user", user);
		return prefix+"/edit";
	}

	@RequiresPermissions("deviceUser:deviceUser:addUser")
	@Log("保存设备用户")
	@PostMapping("/saveUser")
	@ResponseBody
	R saveUser(TabUser record) {
		if (record.getDeviceId().longValue() == 0) {
			return R.error("请选择设备");
		}
		
		TabDevice device = deviceService.selectByPrimaryKey(record.getDeviceId());
		if (device == null) {
			return R.error("设备不存在");
		}
		record.setDeviceTypeId(device.getDeviceTypeId());
		record.setCreateTime(new Date());
		if (userService.insertSelective(record) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("deviceUser:deviceUser:updateUser")
	@Log("更新设备用户")
	@PostMapping("/update")
	@ResponseBody
	R update(TabUser record) {
		if (record.getDeviceId().longValue() == 0) {
			return R.error("请选择设备");
		}
		List<TabUser> list = userService.selectByAccount(record.getAccount());
		if (list != null) {
			for (TabUser tabUser : list) {
				if (record.getId().longValue() != tabUser.getId().longValue()) {
					return R.error("设备用户名称已经存在");
				}
			}
		}
		TabDevice device = deviceService.selectByPrimaryKey(record.getDeviceId());
		if (device == null) {
			return R.error("设备不存在");
		}
		record.setDeviceTypeId(device.getDeviceTypeId());
		if (userService.updateByPrimaryKeySelective(record) > 0) {
			return R.ok();
		}
		return R.error();
	}

	@RequiresPermissions("deviceUser:deviceUser:deleteUser")
	@Log("删除设备用户")
	@PostMapping("/deleteUser")
	@ResponseBody
	R deleteUser(Long id) {
		
		if (userService.deleteByPrimaryKey(id) > 0) {
			// 删除用户消息
			userMsgService.deleteUserMsgByUserId(id);
			
			return R.ok();
		}
		return R.error();
	}

	@PostMapping("/exitAccount")
	@ResponseBody
	boolean exitAccount(@RequestParam Map<String, Object> params) {
		return !userService.exitAccount(params);
	}
}
